package com.mindbook.vip.gawxchatinfo.service.impl.nostrum;

import com.mindbook.vip.gawxchatinfo.entity.nostrum.PrimaryLiverCancerTreatment;
import com.mindbook.vip.gawxchatinfo.mapper.nostrum.PrimaryLiverCancerTreatmentMapper;
import com.mindbook.vip.gawxchatinfo.service.nostrum.PrimaryLiverCancerTreatmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @NAME: wxb
 * @DATE: 2022/8/7
 * @DESCRIPTION:
 **/
@Service
public class PrimaryLiverCancerTreatmentServiceImpl implements PrimaryLiverCancerTreatmentService {

    @Autowired private PrimaryLiverCancerTreatmentMapper mapper;

    @Override
    public Integer removeByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Integer add(PrimaryLiverCancerTreatment primaryLiverCancerTreatment) {
        return mapper.insertSelective(primaryLiverCancerTreatment);
    }

    @Override
    public Integer put(PrimaryLiverCancerTreatment primaryLiverCancerTreatment) {
        return null;
    }

    @Override
    public Integer addSelective(PrimaryLiverCancerTreatment primaryLiverCancerTreatment) {
        return null;
    }

    @Override
    public PrimaryLiverCancerTreatment findByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public Integer saveByPrimaryKeySelective(PrimaryLiverCancerTreatment primaryLiverCancerTreatment) {
        return null;
    }

    @Override
    public Integer saveByPrimaryKey(PrimaryLiverCancerTreatment primaryLiverCancerTreatment) {
        return null;
    }

    @Override
    public List<PrimaryLiverCancerTreatment> findAll() {
        return null;
    }

    @Override
    public String businessProcess(PrimaryLiverCancerTreatment p) {
        //全身健康状况ps
        if(p.getPhysicalCondition()>=3){
            return "G10";
        }
        //获取Child-pugh分级为C的状态
        if(childPughGrade(p)){
            //此处是C级分类
            if(extrahepaticMetastasis(p)){
                return "G9";
            }else{
                return "G8";
            }
        }

        //获取child-pugh分级为a、b的状态下有肝外转移
        if(extrahepaticMetastasis(p)){
            return "G7";
        }

        //如果有血管侵犯
        if(vascularInvasion(p)){
            return "G6";
        }

        //如果肿瘤数目大于等于4
        Integer theNumberOfTumor = p.getTheNumberOfTumor();
        Integer tumorSize = p.getTumorSize();
        if(theNumberOfTumor==4){
            return "G5";
        }else if(theNumberOfTumor==1){
            //如果肿瘤数目为1
            if(tumorSize==4){
                return "G2";
            }
            return "G1";
        }else{
            //肿瘤数目为2或者3
            if(tumorSize<=2){
                return "G3";
            }
            return "G4";
        }
    }

    /**
     * 血管侵犯
     * 有任何一种侵犯，即为血管侵犯
     * @param p
     * @return
     */
    private boolean vascularInvasion(PrimaryLiverCancerTreatment p){
        Integer mainHepativVeinInvasion = p.getMainHepaticVeinInvasion();
        Integer mainPortalVeinInvasion = p.getMainPortalVeinInvasion();
        if(mainHepativVeinInvasion+mainPortalVeinInvasion>0){
            return true;
        }
        return false;
    }

    /**
     * 肝外转移的判断
     * 如果参数相加大于零，则为true
     * @param p
     * @return
     */
    private boolean extrahepaticMetastasis(PrimaryLiverCancerTreatment p){
        Integer lymphaticMetastasis = p.getLymphaticMetastasis();
        Integer distantMetastasis = p.getDistantMetastasis();
        if(lymphaticMetastasis+distantMetastasis>0){
            return true;
        }
        return false;
    }



    /**
     * 计算child-pugh 评分。但业务处理中，将a与b评分归为了一类，所以，此处校验是否为c类评分
     * @param p
     * @return
     */
    private boolean childPughGrade(PrimaryLiverCancerTreatment p){
        Integer totalPoints = 0;
        //肝性脑病
        Integer hepaticEncephalopathy = hepaticEncephalopathy(p.getHepaticEncephalopathy());
        //腹水
        Integer theDegreeOfAscites = theDegreeOfAscites(p.getTheDegreeOfAscites());
        //总胆红素(总胆红素的初始值就是1)
        Integer totalBilirubin = p.getTotalBilirubin();
        //白蛋白
        Integer serumAlbumin = p.getSerumAlbumin();
        //凝血酶原延长时间
        Integer prolongedClottingTime = p.getProlongedClottingTime();
        //现在总计Child-pugh评分
        totalPoints = hepaticEncephalopathy + theDegreeOfAscites + totalBilirubin + serumAlbumin + prolongedClottingTime;
        if(totalPoints >=10){
            return true;
        }
        return false;
    }

    /**
     * 腹水程度
     *
     * @param value
     * @return
     */
    private Integer theDegreeOfAscites(Integer value) {
        int points = 0;
        switch (value) {
            case 0:
                points = 1;
                break;
            case 1:
                points = 2;
                break;
            case 2:
            case 3:
                points = 3;
                break;
        }
        return points;
    }

    /**
     * 计算肝性脑病的评分
     *
     * @param value
     * @return
     */
    private Integer hepaticEncephalopathy(Integer value) {
        int points = 0;
        switch (value) {
            case 0:
                points = 1;
                break;
            case 1:
            case 2:
                points = 2;
                break;
            case 3:
            case 4:
                points = 3;
                break;
        }
        return points;
    }




}
